# -*- coding:utf-8 -*-
import socket
import threading


def handle_request(new_client):
    """
    处理用户请求
    :param new_client: 用户
    :return:
    """
    recv_data = new_client.recv(4096).decode('gbk')
    print('Received: %s' % recv_data)
    new_client.send('haha'.encode('utf-8'))


def main():
    """
    完成服务器的主要控制
    """
    # 1. 创建套接字
    tcp_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 2. 绑定本地信息
    tcp_server.bind(('', 7890))
    # 3. 让默认套接字由主动变被动 listen
    tcp_server.listen(128)
    while True:
        # 3.等待客户端链接 accept
        new_client, client_addr = tcp_server.accept()
        print('Accepted from Connect: %s:%d' % (client_addr[0], client_addr[1]))
        # 4. 处理客户端请求
        client_handle = threading.Thread(target=handle_request, args=(new_client,))
        client_handle.start()


if __name__ == '__main__':
    main()



